// 获取当前日期
export function getNowFormatDate() {
  var date = new Date();
  var seperator1 = "/";
  var year = date.getFullYear();
  var month = date.getMonth() + 1;
  var strDate = date.getDate();
  if (month >= 1 && month <= 9) {
    month = "0" + month;
  }
  if (strDate >= 0 && strDate <= 9) {
    strDate = "0" + strDate;
  }
  var currentdate = year + seperator1 + month + seperator1 + strDate;
  return currentdate;
}

// 根据 后端给的 datetime 字符串 转时间
export function transformDate(datetime) {
  if (datetime != null && datetime !== "") {
    let date = new Date(datetime);
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    if (month >= 1 && month <= 9) {
      month = "0" + month;
    }
    var strDate = date.getDate();
    if (strDate >= 0 && strDate <= 9) {
      strDate = "0" + strDate;
    }
    return `${year}-${month}-${strDate}`;
  } else {
    return "";
  }
}

/**
 *
 * 判断两个ip是否在同一网段
 * 用于网关配置判断vrrp1和vrrp2分别是否和网关ip在同一网段内
 * gwAddr:网关ip/掩码
 * vrrp:vrrp1或者vrrp2
 * 返回值:true在同一网段/false不在同一网段
 *
 **/
export function isEqualIPAddress(gwAddr, vrrp) {
  if (!gwAddr || !vrrp) {
    return false;
  }
  let gwAddrs = gwAddr.split("/");
  let gwIp = gwAddrs[0];
  let mask = gwAddrs[1];
  if (!gwIp || !mask) {
    return false;
  }
  let res1 = [];
  let res2 = [];
  gwIp = gwIp.split(".");
  vrrp = vrrp.split(".");
  let o = "00000000";
  for (var i = 0, ilen = gwIp.length; i < ilen; i += 1) {
    let num1 = parseInt(gwIp[i]).toString("2");
    if (num1.length !== 8) {
      let site = 8 - num1.length;
      num1 = o.slice(0, site) + num1;
    }
    let num2 = parseInt(vrrp[i]).toString("2");
    if (num2.length !== 8) {
      let site = 8 - num2.length;
      num2 = o.slice(0, site) + num2;
    }
    res1.push(num1);
    res2.push(num2);
  }
  if (
    res1.join("").slice(0, parseInt(mask)) ===
    res2.join("").slice(0, parseInt(mask))
  ) {
    return true;
  } else {
    return false;
  }
}
